//
//  ViedoViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 Hero. All rights reserved.
//

#import "ViedoViewController.h"

@interface ViedoViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *scrollerArray;
@property (nonatomic,strong)NSMutableArray *dataSource;

@end

@implementation ViedoViewController

- (NSMutableArray *)scrollerArray{
    if (!_scrollerArray) {
        _scrollerArray = [[NSMutableArray alloc]init];
    }
    return _scrollerArray;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [[NSMutableArray alloc]init];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ViedoCell" bundle:nil] forCellReuseIdentifier:@"ViedoModel_cell"];
    
    //数据请求
    [RequestManager fetchDataWithUrl:kViedoUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];


        
        for (NSDictionary *smallDic in bigDic[@"data"][@"carousel"]) {
            [self.scrollerArray addObject:smallDic[@"img"]];
        }
        for (NSDictionary *dic in bigDic[@"data"][@"alllist"]) {
            ViedoModel *model = [[ViedoModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSource addObject:model];
            
            
        }
       
    } updata:^{
        [self.tableView reloadData];
        //创建轮播图
        LSDScrollView *scrollView = [[LSDScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)imageArray:self.scrollerArray];
        
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3)];
        headerView.backgroundColor = [UIColor yellowColor];
        self.tableView.tableHeaderView = headerView;
        self.edgesForExtendedLayout = UIRectEdgeNone;  //防止跑偏
        [self.tableView.tableHeaderView addSubview:scrollView];
    }];
    

}

#pragma mark---------tableViewDelegate--------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ViedoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ViedoModel_cell" forIndexPath:indexPath];
    
    [cell setCellWithModel:self.dataSource[indexPath.row]];
  
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ViedoListViewController *viedoVC = [[ViedoListViewController alloc]init];
    

    viedoVC.backTitle = [self.dataSource[indexPath.row] title];
    viedoVC.radionID  = [self.dataSource[indexPath.row] radioid];
    
    [self.navigationController pushViewController:viedoVC animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
